home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / or / or_or2rbits.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  6.6 KB  |  344 lines

  1. /* or_or2rbits.c: break or into bits */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/or/RCS/or_or2rbits.c,v 6.0 1991/12/18 20:23:08 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/or/RCS/or_or2rbits.c,v 6.0 1991/12/18 20:23:08 jpo Rel $
  9.  *
  10.  * $Log: or_or2rbits.c,v $
  11.  * Revision 6.0  1991/12/18  20:23:08  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "or.h"
  19. #include "util.h"
  20. #include "table.h"
  21.  
  22. extern char     *loc_dom_site;
  23. extern char     or_error[];
  24. extern char     *or2rfc_tbl;
  25. static Table    *tb_or2rfc;
  26. static int    or_or2rbits_ckdmn(), or_isOKforRHS();
  27. extern void    fixup_space();
  28.  
  29. /* --- definitions --- */
  30. #define NO_MATCH        0
  31. #define OU_O_MATCH        1
  32. #define PRMD_ADMD_C_MATCH    2
  33.  
  34.  
  35. /* -- local routines -- */
  36. int        or_or2rbits();
  37. static        get_psORasc();
  38.  
  39.  
  40.  
  41.  
  42. /* ---------------------  Begin  Routines  -------------------------------- */
  43.  
  44.  
  45.  
  46.  
  47. /* --- Get local + domain from or --- */
  48.  
  49. #ifdef NOTNEEDED
  50. static char    *at_str = "@";
  51. #endif
  52.  
  53. int or_or2rbits (or, local, domain)
  54. OR_ptr  or;
  55. char    *local;
  56. char    *domain;
  57. {
  58.     OR_ptr          ptr,
  59.             dptr,
  60.             tptr;
  61.     char            buf[LINESIZE],
  62.             tbuf[LINESIZE],
  63.             tmp[LINESIZE],
  64.             loc_ps[LINESIZE], /* --- Printable String --- */
  65.             dom_ps[LINESIZE]; /* --- Printable String --- */
  66.     int        cont, or_form;
  67. #ifdef NOTNEEDED
  68.     int        atmatch = FALSE;
  69. #endif
  70.  
  71.     PP_DBG (("Lib/or_or2rbits()"));
  72.  
  73.     if (tb_or2rfc == NULLTBL)
  74.         if ((tb_or2rfc = tb_nm2struct (or2rfc_tbl)) == NULLTBL)
  75.             return or_lose ("No or2rfc table!");
  76.  
  77.     or_form = or_form_type(or);
  78.     domain[0] = '\0';
  79.     if (or_add_atsigns (&or) == NOTOK)
  80.         return NOTOK;
  81.  
  82.  
  83.     /* --- See how much we can domain check against --- */
  84.  
  85.     for (ptr = or; ptr -> or_next != NULLOR; ptr = ptr -> or_next)
  86.         if (ptr -> or_type > OR_OU)
  87.             break;
  88.  
  89.  
  90.     /* --- pointing to the first non-dom compt --- */
  91.  
  92.     if (ptr != or) {
  93.         dptr = ptr;
  94.         ptr = ptr -> or_prev;
  95.  
  96.         /* --- move until we find a match --- */
  97.  
  98.         for (; ptr != NULLOR; ptr = ptr->or_prev) {
  99.             or_or2dmn (NULLOR, ptr, buf);
  100.  
  101.             if (tb_k2val (tb_or2rfc, buf, tbuf, TRUE) != OK)
  102.                 continue;
  103.  
  104.             /* got a match */
  105.  
  106.             /* --- check that tbuf is a valid domain syntax --- */
  107.             if (or_or2rbits_ckdmn (tbuf) == NOTOK)
  108.                 goto doexplicit;
  109.  
  110.             switch (or_form) {
  111.                 case OR_FORM_TERM:
  112.                 case OR_FORM_POST:
  113.                 case OR_FORM_NUMR:
  114.                 /* fully lhs encode */
  115.                 (void) strcpy (dom_ps, tbuf);
  116.                 get_psORasc (dom_ps, domain);
  117.                 if (index(domain, '.') == NULLCP)
  118.                     /* only one component to domain */
  119.                     /* so LHS encode with loc_dom_site */
  120.                     domain[0] = '\0';
  121.                 goto doexplicit;
  122.                 default:
  123.                 case OR_FORM_MNEM:
  124.                 break;
  125.             }
  126.  
  127.             /* ptr is ones we've matched */
  128.             /* dptr is end of components that may be added to RHS */
  129.             
  130.             ptr = ptr -> or_next;
  131.             cont = TRUE;
  132.             
  133.             (void) strcpy (dom_ps, tbuf);
  134.  
  135.             while (cont == TRUE
  136.                    && ptr != dptr) {
  137.                 if (lexequ (ptr -> or_value, "@") == 0) {
  138.                     /* missing next component */
  139.                     /* lhs encode from here */
  140.                     cont = FALSE;
  141.                     continue;
  142.                 }
  143.                 
  144.                 fixup_space(ptr -> or_value, tmp);
  145.                 if (or_isOKforRHS(tmp)) {
  146.                     (void) sprintf(tbuf, "%s.%s", tmp, dom_ps);
  147.                     (void) strcpy(dom_ps, tbuf);
  148.                     ptr = ptr -> or_next;
  149.                 } else 
  150.                     cont = FALSE;
  151.             }
  152.             
  153.             /* appended what could to rhs */
  154.             /* lhs encode from here */
  155.             
  156.             tptr = ptr -> or_prev;
  157.             ptr -> or_prev = NULLOR;
  158.             
  159.             if (or_delete_atsigns (&ptr) == NOTOK)
  160.                 return NOTOK;
  161.  
  162.             or_or2std (ptr, loc_ps, TRUE);
  163.             if (loc_ps[strlen (loc_ps) - 1]  == '/')
  164.                 or_or2std (ptr, loc_ps, FALSE);
  165.  
  166.  
  167.             ptr -> or_prev = tptr;
  168.             tptr -> or_next = ptr;
  169.  
  170.             get_psORasc (dom_ps, domain);
  171.  
  172.             if (index(domain, '.') == NULLCP) {
  173.                 /* only one component to domain */
  174.                 /* so LHS encode */
  175.                 domain[0] = '\0';
  176.                 goto doexplicit;
  177.             }
  178.             get_psORasc (loc_ps, local);
  179.  
  180.             PP_DBG ((
  181.             "Lib/or_or2rbits DMN encoded local='%s', dmn='%s'",
  182.             local, domain
  183.             ));
  184.  
  185.             if (or_delete_atsigns (&or) == NOTOK)
  186.                 return NOTOK;
  187.  
  188.             return OK;
  189.  
  190.         }   /* end of for */
  191.  
  192.     } /* end of if */
  193.  
  194.  
  195. doexplicit:
  196.     /* --- Do full LHS encoding --- */
  197.     if (or_delete_atsigns (&or) == NOTOK)
  198.         return NOTOK;
  199.  
  200.     if (domain[0] == '\0')
  201.         (void) strcpy (domain, loc_dom_site);
  202.     or_or2std (or, loc_ps, FALSE);
  203.     get_psORasc (loc_ps, local);    
  204.  
  205.     PP_DBG (("Lib/or_or2rbits/Default setting local='%s' domain='%s'",
  206.         local, domain));
  207.  
  208.     return OK;
  209. }
  210.  
  211.  
  212.  
  213.  
  214. /* --------------------------  Static  Routines  ---------------------------- */
  215.  
  216. static int or_isOKforRHS (dmn)
  217. char    *dmn;
  218. {
  219.     char    asc[BUFSIZ], *ix;
  220.     get_psORasc(dmn, asc);
  221.  
  222.     if (!isalnum(asc[0]) || 
  223.         !isalnum(asc[strlen(asc)-1]))
  224.         return FALSE;
  225.         
  226.     for (ix = &asc[0]; 
  227.          ix != NULL && *ix != '\0';
  228.          ix++)
  229.         if (!isalnum(*ix) 
  230.             && *ix != '-')
  231.             return FALSE;
  232.     return TRUE;
  233. }
  234.  
  235. static int or_or2rbits_ckdmn (dmn)
  236. char    *dmn; 
  237. {
  238.     char    buf[LINESIZE];
  239.     char    *ptr = dmn;
  240.  
  241.     PP_DBG (("Lib/or_or2rbits_ckdmn (%s)", dmn));
  242.  
  243.     buf[0] = '\0'; 
  244.  
  245.     while (or_gettoken (&ptr, '.', buf) == OK)
  246.         if (!or_isOKforRHS (buf))
  247.             return or_lose ("Bad style RFC-822 domain '%s'", dmn);
  248.     return OK;
  249.  
  250.  
  251.  
  252. #ifdef NOTNEEDED
  253. static int or_or2rbits_match (start, dmn)
  254. OR_ptr  start;
  255. OR_ptr  *dmn;
  256. {
  257.     char    buf[LINESIZE];
  258.     OR_ptr  curr;
  259.  
  260.  
  261.     /* --- *** ---
  262.     Build domain - match. If attribute value does not hold a valid domain 
  263.     syntax, then move that part to the local part. This is only done if 
  264.     no mapping for that attribute has been specified in the or2rfc table.
  265.     Example:
  266.         '/s=user/ou=org unit/prmd=valid/admd=gold 400/c=gb/'
  267.     will be RFC987 mapped to:
  268.         '"/s=user/ou=org unit/"@valid.gold-400.gb'
  269.     --- *** --- */
  270.  
  271.     for (curr = *dmn; curr != start; curr = curr -> or_prev) {
  272.         get_psORasc (curr -> or_value, buf);
  273.         fixup_space(buf, buf);
  274.         if ((!or_isOKforRHS (buf)) && (lexequ (buf, "@") != 0)) {
  275.             if (curr -> or_prev)
  276.                 *dmn = curr -> or_prev;
  277.             else
  278.                 return or_lose ("No OR prev '%s'",
  279.                         curr->or_value);
  280.         }
  281.     }
  282.  
  283.     return OK;
  284. }
  285.  
  286. /* --- match for "@" signs in the or2rfc table --- */
  287.  
  288. static int or_or2rbits_atsigns (or)
  289. OR_ptr    or;
  290. {
  291.     OR_ptr    next = or -> or_next;    /* save the value */
  292.     int    type = or -> or_type;
  293.     OR_ptr    at;
  294.     char    key[LINESIZE], value[LINESIZE]; 
  295.     int    retval;
  296.  
  297.  
  298.     if (type < OR_OU)
  299.         type++;
  300.     else
  301.         return NO_MATCH;
  302.  
  303.     at = or_new (type, NULLCP, "@");
  304.  
  305.     at -> or_prev = or;
  306.     at -> or_next = NULLOR;
  307.     or -> or_next = at;    
  308.  
  309.  
  310.     or_or2dmn (NULLOR, at, key);    
  311.  
  312.     retval = tb_k2val (tb_or2rfc, key, value, TRUE);
  313.  
  314.     or -> or_next = next;
  315.  
  316.     at -> or_prev = at -> or_next = NULLOR;
  317.     or_free (at);
  318.  
  319.     if (retval != OK)
  320.         return NO_MATCH;
  321.  
  322.     if (type < OR_O)
  323.         return PRMD_ADMD_C_MATCH;
  324.  
  325.     return OU_O_MATCH;
  326. }
  327. #endif
  328.  
  329.  
  330. static get_psORasc (value, buf)
  331. char    *value;
  332. char    *buf;
  333. {
  334.  
  335. #ifndef STRICT_1148
  336.     if (!(value[0] == '/'
  337.         && index(value, '=') != NULLCP))
  338.         (void) or_ps2asc (value, buf);
  339.     else
  340. #endif
  341.     (void) strcpy (buf, value);
  342. }
  343.